package fun.carliels.doraemon.common.random;

import org.apache.commons.lang3.ArrayUtils;

import java.security.SecureRandom;
import java.util.HashSet;
import java.util.Set;

/**
 * 随机字符串工具类
 * @author 残血拉二胡
 */
public class RandomStrKit {

    /**
     * 可以根据需要加入小写英文字母和特殊字符等
     */
    private static final char[] LETTER_SOURCE = new char[]{
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
            'U', 'V', 'W', 'X', 'Y', 'Z',
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
            'u', 'v', 'w', 'x', 'y', 'z'
    };
    private static final char[] NUM_SOURCE = new char[] {
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
    };

    private static final char[] CHAR_SOURCE = new char[] {
            '~', '!', '@', '#', '$', '%', '^', '&', '*','+', '-', '=', '?'
    };

    private static final char[] LETTER_NUMBER_SOURCE = ArrayUtils.addAll(LETTER_SOURCE, NUM_SOURCE);

    private static final char[] LETTER_NUMBER_CHAR_SOURCE = ArrayUtils.addAll(LETTER_NUMBER_SOURCE, CHAR_SOURCE);

    private static final SecureRandom random = new SecureRandom();


    /**
     * 随机字母加数字
     * @param len
     * @return
     */
    public static final String genLetterNumberChar(int len) {
        if (len < 1) {
            return null;
        }
        return gen(LETTER_NUMBER_CHAR_SOURCE, len);
    }


    /**
     * 随机字母加数字
     * @param len
     * @return
     */
    public static final String genLetterNumber(int len) {
        if (len < 1) {
            return null;
        }
        return gen(LETTER_NUMBER_SOURCE, len);
    }


    /**
     * 随机字母
     * @param len  长度
     * @return
     */
    public static final String genLetter(int len) {
        if (len < 1) {
            return null;
        }
        return gen(LETTER_SOURCE, len);
    }

    /**
     * 随机数字
     * @param len
     * @return
     */
    public static final String genNumber(int len) {
        if (len < 1) {
            return null;
        }
        return gen(NUM_SOURCE, len);
    }

    /**
     * 生成随机字符串
     * @param source 字符源
     * @param len 长度
     * @return
     */
    private static String gen(char[] source, int len) {
        if (len < 1) {
            return null;
        }
        char[] tmp = new char[len];
        for (int i = 0; i < len; i++) {
            final int index = random.nextInt(source.length);
            tmp[i]=source[index];
        }
        shuffle(tmp);
        return new String(tmp);
    }

    /**
     * 打乱顺序
     * @param arr
     */
    private static void shuffle(char[] arr) {
        for (int i = arr.length; i > 0; i--) {
            int rand = random.nextInt(i);
            char temp = arr[rand];
            arr[rand] = arr[i-1];
            arr[i-1] = temp;
        }
    }

    public static void main(String[] args) {
        Set<String> collection = new HashSet<>(100);
        for (int i=0; i<10000000; i++) {
            String code = genLetterNumber(6);
            collection.add(code);
            System.out.println(code);
        }
        System.out.println(collection.size());
    }
}
